home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / utility / date2amiga.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  3KB  |  96 lines

  1. /*
  2.     $Id: date2amiga.c,v 1.1 1996/08/31 12:58:12 aros Exp $
  3.     $Log: date2amiga.c,v $
  4.     Revision 1.1  1996/08/31 12:58:12  aros
  5.     Merged in/modified for FreeBSD.
  6.  
  7.     Desc:
  8.     Lang: english
  9. */
  10. #include "utility_intern.h"
  11.  
  12. /*****************************************************************************
  13.  
  14.     NAME */
  15.         #include <clib/utility_protos.h>
  16.  
  17.         __AROS_LH1(ULONG, Date2Amiga,
  18.  
  19. /*  SYNOPSIS */
  20.         __AROS_LHA(struct ClockData *, date, A0),
  21.  
  22. /*  LOCATION */
  23.         struct UtilityBase *, UtilityBase, 21, Utility)
  24.  
  25. /*  FUNCTION
  26.         Converts the information given in the struct ClockData *date, into
  27.         the number of seconds that have past since the 1st of January 1978.
  28.  
  29.     INPUTS
  30.         date    -   Contains the information about the time.
  31.  
  32.     RESULT
  33.         The number of seconds since 1.1.1978
  34.  
  35.     NOTES
  36.  
  37.     EXAMPLE
  38.  
  39.     BUGS
  40.  
  41.     SEE ALSO
  42.         utility/Amiga2Date(), utility/CheckData()
  43.  
  44.     INTERNALS
  45.         Bit of a hack in the leap year handling.
  46.  
  47.     HISTORY
  48.         29-10-95    digulla automatically created from
  49.                             utility_lib.fd and clib/utility_protos.h
  50.  
  51. *****************************************************************************/
  52. {
  53.     __AROS_FUNC_INIT
  54.  
  55.     /* This array contains the number of days that have been in the year
  56.        up to the start of the month. Does not take into account leap years.
  57.     */
  58.     static const UWORD dayspermonth[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  59.  
  60.     ULONG time;
  61.     UWORD year;
  62.  
  63.     time = date->sec + (date->min * 60) + (date->hour * 3600);
  64.     time += (date->mday - 1)* 86400;
  65.     time += dayspermonth[date->month-1] * 86400;
  66.     time += (date->year - 1978) * 86400 * 365;
  67.  
  68.     /* Now comes the hard bit, how do we work out the extra day for a leap
  69.         year. I do it by subtracting lots of four to start with, then
  70.         by considering the remaining few years.
  71.  
  72.         For every group of four years which we have to subtract, we can
  73.         add one leap year. eg 1982 - 1978= 4, so we add one leap year (1980)
  74.  
  75.         1989 - 1978 = 11 (two lots of four subtracted), (1980, 1984, 1988).
  76.         However in this case, the 1988 is handled by the code after the
  77.         while loop.
  78.  
  79.         Is there an easier way perhaps?
  80.     */
  81.     year = date->year - 1978;
  82.     while( year > 3 )
  83.     {
  84.         if(year > 4 )   time += 86400;
  85.         year -= 4;
  86.     }
  87.  
  88.  
  89.     if( (year > 2) || ((year == 2) && (date->month > 2)))
  90.         time += 86400;
  91.  
  92.     return time;
  93.  
  94.     __AROS_FUNC_EXIT
  95. } /* Date2Amiga */
  96.